<script setup lang="ts">
import { getCategoryTopAPI } from '@/services/category'
import { getHomeBannerAPI } from '@/services/home'
import type { CategoryTopItem } from '@/types/category'
import { onLoad, onShow, onUnload } from '@dcloudio/uni-app'
import { computed, ref } from 'vue'
import PageSkleton from './components/PageSkleton.vue'
import type { Banners } from '@/api'
import CustomNavbar from '../index/components/CustomNavbar.vue'
import HtzRate from '@/components/htz-rate/htz-rate.vue'
//

const bannerList = ref<Banners[]>([])
const getBannerData = async () => {
  const res = await getHomeBannerAPI(2)
  bannerList.value = res.data
}

//获取分页列表数据
const categoryList = ref<CategoryTopItem[]>([])
const getCategoryTopData = async () => {
  const res = await getCategoryTopAPI()
  categoryList.value = res.data
}

//记录数据加载完毕
const isFinished = ref(false)

//实现tap交互的变量
const activeIndex = ref(0)
onLoad(async () => {
  uni.$on('refreshPage', () => {
    //监听刷新此页面数据的事件，在评分成功后，触发回调
    console.log('刷新数据回调触发')
    getCategoryTopData()
  })
  await Promise.all([getBannerData(), getCategoryTopData()])
  isFinished.value = true
})
onUnload(() => {
  uni.$off('refreshPage', () => {
    console.log('refreshPage 监听器已经移除')
  })
})
//提取当前分类二级数据
const subCategoryList = computed(() => {
  return categoryList.value[activeIndex.value]?.children || []
})

//点击图片时跳转详情页
const handleSkipToGood = (id: string) => {
  uni.navigateTo({
    url: `/pages/goods/goods?id=${id}`,
  })
}
</script>

<template>
  <view class="viewport" v-if="isFinished">
    <!-- 搜索框 -->
    <CustomNavbar />
    <!-- 分类 -->
    <view class="categories">
      <!-- 左侧：一级分类 -->
      <scroll-view class="primary" scroll-y>
        <view
          v-for="(item, index) in categoryList"
          :key="item.id"
          class="item"
          @tap="activeIndex = index"
          :class="{ active: index === activeIndex }"
        >
          <text class="name"> {{ item.name }} </text>
        </view>
      </scroll-view>
      <!-- 右侧：二级分类 -->
      <scroll-view class="secondary" scroll-y>
        <!-- 焦点图
        <DCSwiper class="banner" :list="bannerList" /> -->
        <!-- 内容区域 -->
        <view class="panel" v-for="item in subCategoryList" :key="item.id">
          <view class="title">
            <text class="name">{{ item.name }}</text>
            <navigator class="more" hover-class="none">全部</navigator>
          </view>
          <view class="section">
            <view v-for="goods in item.goodsList" :key="goods.id" class="goods" hover-class="none">
              <view class="details">
                <image class="image" :src="goods.picture" @tap="handleSkipToGood(goods.id)"></image>
                <view class="name ellipsis">{{ goods.name }}</view>
                <view class="price">
                  <text class="symbol">¥</text>
                  <text class="number">{{ goods.price }}</text>
                </view>
              </view>
              <view class="rating-area"
                >平均评分：{{ goods.averageScore }}
                <!-- 星级 -->
                <view class="star-rating-container">
                  <HtzRate :value="goods.averageScore" :readonly="true" />
                </view>
                <!-- 评论按钮 -->
                <navigator class="review-button" :url="`/pages/RateScore/RateScore?id=${goods.id}`">
                  评论
                </navigator>
              </view>
            </view>
          </view>
        </view>
      </scroll-view>
    </view>
  </view>
  <view v-else>
    <PageSkleton />
  </view>
</template>

<style lang="scss">
page {
  height: 100%;
  overflow: hidden;
}
.viewport {
  height: 100%;
  display: flex;
  flex-direction: column;
}
.search {
  padding: 0 30rpx 20rpx;
  background-color: #fff;
  .input {
    display: flex;
    align-items: center;
    justify-content: space-between;
    height: 64rpx;
    padding-left: 26rpx;
    color: #8b8b8b;
    font-size: 28rpx;
    border-radius: 32rpx;
    background-color: #f3f4f4;
  }
}
.icon-search {
  &::before {
    margin-right: 10rpx;
  }
}
/* 分类 */
.categories {
  flex: 1;
  min-height: 400rpx;
  display: flex;
}
/* 一级分类 */
.primary {
  overflow: hidden;
  width: 180rpx;
  flex: none;
  background-color: #f6f6f6;
  .item {
    display: flex;
    justify-content: center;
    align-items: center;
    height: 96rpx;
    font-size: 26rpx;
    color: #595c63;
    position: relative;
    &::after {
      content: '';
      position: absolute;
      left: 42rpx;
      bottom: 0;
      width: 96rpx;
      border-top: 1rpx solid #e3e4e7;
    }
  }
  .active {
    background-color: #fff;
    &::before {
      content: '';
      position: absolute;
      left: 0;
      top: 0;
      width: 8rpx;
      height: 100%;
      background-color: #27ba9b;
    }
  }
}
.primary .item:last-child::after,
.primary .active::after {
  display: none;
}
/* 二级分类 */
.secondary {
  background-color: #fff;
  .carousel {
    height: 200rpx;
    margin: 0 30rpx 20rpx;
    border-radius: 4rpx;
    overflow: hidden;
  }
  .panel {
    margin: 0 30rpx 0rpx;
  }
  .title {
    height: 60rpx;
    line-height: 60rpx;
    color: #333;
    font-size: 28rpx;
    border-bottom: 1rpx solid #f7f7f8;
    .more {
      float: right;
      padding-left: 20rpx;
      font-size: 24rpx;
      color: #999;
    }
  }
  .more {
    &::after {
      font-family: 'erabbit' !important;
      content: '\e6c2';
    }
  }
  .section {
    width: 100%;
    display: flex;
    flex-wrap: wrap;
    padding: 20rpx 0;
    .goods {
      display: flex; /* 将 goods 设置为 flex 容器 */
      width: 544rpx;
      margin: 0rpx 20rpx 20rpx 0;
      &:nth-child(3n) {
        margin-right: 0;
      }

      .details {
        display: flex;
        flex-direction: column;
        justify-content: space-between; /* 垂直对齐方式为两端对齐 */
        /* 让 details 区域尽可能扩展以填充剩余空间 */
        padding-left: 10rpx; /* 给左侧的商品图片留出一些间距 */

        image {
          width: 150rpx;
          height: 150rpx;
        }
        .name {
          padding: 5rpx;
          font-size: 22rpx;
          color: #333;
          white-space: pre-wrap;
          width: 150rpx;
        }
        .price {
          padding: 5rpx;
          font-size: 18rpx;
          color: #cf4444;
        }
      }
      .rating-area {
        background-color: rgb(148, 143, 143);
        color: black;
        padding: 5rpx;
        text-align: center;
        flex-grow: 1; /* 让评分区尽可能扩展以填充剩余空间 */
        margin-left: 10rpx;
        display: flex;
        flex-direction: column;
        height: 100%; /* 设置固定高度以便于布局 */
        background-color: #f9f9f9;
        border-radius: 10px;
        box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
        .star-rating-container {
          flex: 3; /* 占据3/4的空间 */
          display: flex;
          align-items: center;
          margin-bottom: 15px;
        }

        .review-button {
          // margin-top: 10rpx;
          flex: 1; /* 占据1/4的空间 */
          // background-color: ghostwhite;
          // color: white;
          // text-align: center;
          // line-height: 60rpx; /* 确保文本垂直居中 */
          padding: 8px 20px;
          background-color: #007bff;
          color: #fff;
          font-size: 14px;
          border-radius: 20px;
          text-align: center;
          transition: background-color 0.3s ease;
          &.button-hover {
            background-color: #1cb24c;
          }
        }
      }
    }
  }
}
</style>
